import os from '@packages/lib-os';

export function getIsNeedBottomFit() {
    const { availHeight, availWidth } = window.screen;

    if (window.navigator.userAgent.match(/iphone/i)) {
        return [
            '375-812', // iphonex iphonexs
            '414-896', // iPhone XS Max iPhone XR
        ].includes(`${availWidth}-${availHeight}`);
    }

    return false;
}

export function fullScreenFit(s1 = '', s2 = '', s3 = '') {
    if (os.ios) {
        if (getIsNeedBottomFit()) {
            return s3;
        }

        return s2;
    }

    return s1;
}

export function isPopupWindow() {
    if (document.documentElement.clientWidth === 0 && os.android) return true;
    if (
        window.screen.width > document.documentElement.clientWidth ||
        window.screen.height * 0.9 > document.documentElement.clientHeight
    ) {
        return false;
    }

    return true;
}

export const POPUP_WINDOW = isPopupWindow();

// 为 android 预留状态栏高度
const statusBarHeight = navigator.userAgent.match(/statusbarheight\/(\d+)/i);
const ANDROID_STATUSBAR_HEIGHT = Number(statusBarHeight ? statusBarHeight['1'] : 0) / window.devicePixelRatio;
export const TOP_NAV_HEIGHT = fullScreenFit(44 + ANDROID_STATUSBAR_HEIGHT, 64, 88);

/**
 * 获取顶部虚拟导航高度
 * @export
 * @returns {Number}
 */
export function getTopNavHeight() {
    if (os.isFullscreen && POPUP_WINDOW) {
        return TOP_NAV_HEIGHT;
    }
    return 0;
}

export function computedPosition(value = 0, unit = 'px') {
    const needCalc = unit === 'rem';
    if (os.isFullscreen && POPUP_WINDOW) {
        value = needCalc
            ? `calc(${value}rem + ${TOP_NAV_HEIGHT}px)`
            : `${fullScreenFit(value + 44 + ANDROID_STATUSBAR_HEIGHT, value + 64, value + 88)}${unit}`;
    } else {
        value = `${value}${unit}`;
    }
    return value;
}

if (os.ios) {
    let windowSize = window.innerHeight;
    const needBottomFitClass = (os.breakingWechat || !os.wechat) && getIsNeedBottomFit() ? 'needBottomFit' : '';
    const needVirtualNavClass = os.wechat ? 'needVirtualNav' : ''; // 只有微信才需要底部虚拟导航
    const $html = $('html');

    const addBottomFit = () => {
        window.innerHeight >= 724 && $html.addClass(needBottomFitClass); // 大于724的需要底部适配
    };

    addBottomFit();

    const setSize = (max, min) => {
        $html.data('maxInnerHeight', max);
        $html.data('minInnerHeight', min);
    };

    const handler = () => {
        const { innerHeight: newHeight, innerWidth: IW } = window;

        // 如果高度比宽度小，移除适配，【特指屏幕旋转】
        if (newHeight <= IW) {
            $html.removeClass(`${needVirtualNavClass} ${needBottomFitClass}`);
            return;
        }

        if (newHeight > windowSize) {
            $html.addClass(needVirtualNavClass);
            addBottomFit();
            setSize(newHeight, windowSize);
        } else if (newHeight < windowSize) {
            $html.removeClass(`${needVirtualNavClass} ${needBottomFitClass}`);
            setSize(windowSize, newHeight);
            $(window).trigger('windowHeightToMin');
        }
        windowSize = newHeight;
    };

    $(window).on('resize', handler);

    // 微信回退会出现导航条
    window.addEventListener('popstate', () => {
        handler();
        setTimeout(handler, 200);
    });
}
